Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMD-0182: Consume requested CUs for sBPF failures #182

Merged

Conversation

tao-stones
Copy link
Contributor

No description provided.

@tao-stones tao-stones marked this pull request as ready for review October 4, 2024 17:31
@Benhawkins18 Benhawkins18 changed the title Conditional cu charging SIMD-0182: Conditional cu charging Oct 8, 2024
Comment on lines +98 to +100
If VM execution returns any error except `SyscallError`, transaction's CU meter
should be fully depleted, in another words, all requested CUs are consumed;
otherwise consumes the actual executed CUs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, why make syscall errors exempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is syscall not happen in VM realm. @Lichtso @ptaffet-jump please correct me if I'm wrong.

Copy link

@topointon-jump topointon-jump Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Syscall errors are never thrown in the middle of basic blocks, so we can easily know exactly how many CUs were consumed when the error was thrown. This is why we can safely make them exempt.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the usual definition of basic blocks, call instructions can occur anywhere in a basic block (apart from the last instruction). So I would say this is unclear.

Also, it does not explain the need for the exemption. I would expect all CUs to be all consumed if a program failed, including if that failure was in a syscall.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect all CUs to be all consumed if a program failed, including if that failure was in a syscall.

Why would you expect that? If a program makes a CPI and that invoked program returns an error, we immediately stop executing the transaction and therefore have more time to execute other transactions in a block. If we consume all remaining CUs for that failure, we are wasting that capacity for execution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanyoung: This is mostly about how easy / hard it is to pinpoint the precise fault location. During syscalls the VM has stopped anyway and the return address is known on the stack. Otherwise it can be any location since the last metering (which happens at controlflow instructions). Also syscall errors are far more common and expected to happen regularly, whereas other faults such as access violation only happen in broken programs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lichtso that makes sense 👍

proposals/simd-0182-conditional-cu-metering.md Outdated Show resolved Hide resolved
proposals/simd-0182-conditional-cu-metering.md Outdated Show resolved Hide resolved
proposals/simd-0182-conditional-cu-metering.md Outdated Show resolved Hide resolved

## Detailed Design

If VM execution returns any error except `SyscallError`, transaction's CU meter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that with direct mapping enabled, we convert EbpfError::AccessViolation into the SyscallError variant here: https://github.com/anza-xyz/agave/blob/af0ed22174999cb62579a0621f6b274c85ebf267/programs/bpf_loader/src/lib.rs#L1501

I am assuming that this access violation should also deplete compute units and should not be considered as an exception right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, same reasoning as: #182 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should consume all cu's or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should. This is not caused by a syscall so the exact instruction counter would have to be calculated otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to call out that this type of access violation is probably going to be fairly common when direct mapping is enabled. So it's not exactly true to say that these types of errors are "rare, exceptional situations" or "A rare case" as we say in the SIMD. We should update the SIMD to say that these types of failures are "less common" rather than rare at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that with direct mapping enabled, we convert EbpfError::AccessViolation into the SyscallError variant here: https://github.com/anza-xyz/agave/blob/af0ed22174999cb62579a0621f6b274c85ebf267/programs/bpf_loader/src/lib.rs#L1501

I am assuming that this access violation should also deplete compute units and should not be considered as an exception right?

@tao-stones this should probably be made explicit in the SIMD. Maybe we can say that all EBPF errors from the vm are treated as irregular.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, imo, SIMD's text If VM execution returns any error except SyscallError, .... is clearer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that a spec should be based on "arbitrary" rust types.
We should clarify what's the expected behavior, for example if there's a memory issue inside a syscall, is that a Syscall or Epbf error?

I agree that if we enter a syscall and that fails, we know the exact number of CUs consumed, and so there's no need to deplete them all. I'm not sure if checking the SyscallError type is the correct implementation though. (In the example above, imo a memory issue inside a syscall should NOT deplete, however I don't know if the error thrown is SyscallError or EbpfError)

@tao-stones tao-stones changed the title SIMD-0182: Conditional cu charging SIMD-0182: Consume requested CUs for sBPF failures Nov 21, 2024
Copy link

@topointon-jump topointon-jump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome


## Detailed Design

If VM execution returns any error except `SyscallError`, transaction's CU meter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that with direct mapping enabled, we convert EbpfError::AccessViolation into the SyscallError variant here: https://github.com/anza-xyz/agave/blob/af0ed22174999cb62579a0621f6b274c85ebf267/programs/bpf_loader/src/lib.rs#L1501

I am assuming that this access violation should also deplete compute units and should not be considered as an exception right?

@tao-stones this should probably be made explicit in the SIMD. Maybe we can say that all EBPF errors from the vm are treated as irregular.

Copy link

@ravyu-jump ravyu-jump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@t-nelson
Copy link
Contributor

should this be merged? there is already an implementation in agave master and backport PRs open

@bw-solana
Copy link

I support merging for Agave. CC @Benhawkins18

@Benhawkins18 Benhawkins18 self-requested a review December 11, 2024 20:28
Copy link
Collaborator

@Benhawkins18 Benhawkins18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approvals from both Jump and Anza. Merging

@Benhawkins18 Benhawkins18 merged commit 51e9a72 into solana-foundation:main Dec 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.